package net.mingsoft.basic.util;

import org.apache.commons.lang3.*;
import cn.hutool.core.util.*;
import java.util.regex.*;
import java.util.*;
import java.sql.*;
import java.io.*;

public class StringUtil
{
    public static boolean checkLength(final String str, final int minLength, final int maxLength) {
        if (str == null) {
            return false;
        }
        final int len = str.length();
        if (minLength == 0) {
            return len <= maxLength;
        }
        if (maxLength == 0) {
            return len >= minLength;
        }
        return len >= minLength && len <= maxLength;
    }
    
    public static boolean isIntegers(final String[] str) {
        try {
            for (int i = 0; i < str.length; ++i) {
                Integer.parseInt(str[i]);
            }
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }
    
    public static Integer[] stringsToIntegers(final String[] str) {
        final Integer[] array = new Integer[str.length];
        for (int i = 0; i < array.length; ++i) {
            array[i] = Integer.parseInt(str[i]);
        }
        return array;
    }
    
    public static int[] stringsToInts(final String[] str) {
        final int[] array = new int[str.length];
        for (int i = 0; i < array.length; ++i) {
            array[i] = Integer.parseInt(str[i]);
        }
        return array;
    }
    
    public static String isoToUTF8(final String str) {
        if (StringUtils.isEmpty((CharSequence)str)) {
            return "";
        }
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        }
        catch (UnsupportedEncodingException ex) {
            return "";
        }
    }
    
    public static double[] stringsToDoubles(final String[] str) {
        final double[] array = new double[str.length];
        for (int i = 0; i < array.length; ++i) {
            array[i] = Double.parseDouble(str[i]);
        }
        return array;
    }
    
    public static boolean isDoubles(final String[] str) {
        try {
            for (int i = 0; i < str.length; ++i) {
                Double.parseDouble(str[i]);
            }
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }
    
    public static String getDateSimpleStr() {
        return String.valueOf(System.currentTimeMillis());
    }
    
    @Deprecated
    public static String randomNumber(final int len) {
        final StringBuffer sb = new StringBuffer();
        final Random random = new Random();
        for (int i = 0; i < len; ++i) {
            sb.append(Math.abs(random.nextInt()) % 10);
        }
        return sb.toString();
    }
    
    @Deprecated
    public static String int2String(final int comment) {
        String srt = "";
        srt = Integer.toString(comment);
        return srt;
    }
    
    public static String removeRepeatStr(final String content, final String target) {
        final StringBuffer sb = new StringBuffer(content);
        for (int i = 0; i < sb.length() - 1; ++i) {
            if (sb.substring(i, i + target.length()).equals(target) && sb.substring(i, i + target.length()).equals(sb.substring(i + 1, i + target.length() + 1))) {
                sb.delete(i, i + target.length());
                if (i + target.length() + 1 > sb.length()) {
                    break;
                }
                --i;
            }
        }
        return sb.toString();
    }
    
    public static String sort(final String str, final String delimiter) {
        final String[] articleTypeArrays = str.split(delimiter);
        Arrays.sort(articleTypeArrays);
        return ArrayUtil.join((Object[])articleTypeArrays, (CharSequence)delimiter);
    }
    
    public static boolean isMobile(String phoneNumber) {
        phoneNumber = phoneNumber.trim();
        final String pattern = "^[1][1-9][0-9]{9}";
        return phoneNumber.matches(pattern);
    }
    
    public static Boolean isEmail(final String email) {
        boolean tag = true;
        final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        final Pattern pattern2 = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
        final Matcher mat = pattern2.matcher(email);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }
    
    public static Boolean isIP(final String ip) {
        boolean tag = true;
        final String exg = "(\\d*\\.){3}\\d*";
        final Pattern pattern = Pattern.compile("(\\d*\\.){3}\\d*");
        final Matcher mat = pattern.matcher(ip);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }
    
    public static boolean isInteger(final Object str) {
        if (isBlank(str)) {
            return false;
        }
        try {
            Integer.parseInt(str.toString());
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }
    
    public static String buildPath(final Object... params) {
        String temp = "";
        for (final Object o : params) {
            temp = temp + File.separator + o;
        }
        return temp;
    }
    
    public static String buildUrl(String url, final String parm) {
        if (url.indexOf("?") > 0) {
            return url = url + "&" + parm;
        }
        return url = url + "?" + parm;
    }
    
    public static String buildUrl(String url, final Map parms) {
        final Iterator key = parms.keySet().iterator();
        String paramsStr = "";
        while (key.hasNext()) {
            final Object temp = key.next();
            if (isBlank(parms.get(temp))) {
                continue;
            }
            if (paramsStr != "") {
                paramsStr += "&";
            }
            paramsStr = paramsStr + temp + "=" + parms.get(temp);
        }
        if (paramsStr == "") {
            return url;
        }
        if (url.indexOf("?") > 0) {
            return url = url + "&" + paramsStr;
        }
        return url = url + "?" + paramsStr;
    }
    
    public static String getFileFix(final String filePath) {
        String temp = "";
        if (filePath != null) {
            temp = filePath.substring(filePath.indexOf("."), filePath.length());
        }
        return temp;
    }
    
    @Deprecated
    public static boolean isBlank(final Object obj) {
        return obj == null || obj.toString().trim().equals("") || obj.toString().length() < 0;
    }
    
    public static String clobStr(final Clob clob) {
        if (clob == null) {
            return null;
        }
        String content = "";
        try {
            final Reader is = clob.getCharacterStream();
            final BufferedReader buff = new BufferedReader(is);
            String line = buff.readLine();
            final StringBuffer sb = new StringBuffer();
            while (line != null) {
                sb.append(line);
                line = buff.readLine();
            }
            content = sb.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }
}
